{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "install the necessary library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip -q install transformers openai tiktoken langchain chromadb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "测试luotuo_openai_embedding\n",
    "\n",
    "在utils.py的luotuo_openai_embedding的代码如下\n",
    "\n",
    "```python\n",
    "def luotuo_openai_embedding(texts, is_chinese= None ):\n",
    "    \"\"\"\n",
    "        when input is chinese, use luotuo_embedding\n",
    "        when input is english, use openai_embedding\n",
    "        texts can be a list or a string\n",
    "        when texts is a list, return a list of embeddings, using batch inference\n",
    "        when texts is a string, return a single embedding\n",
    "    \"\"\"\n",
    "\n",
    "    if isinstance(texts, list):\n",
    "        index = random.randint(0, len(texts) - 1)\n",
    "        if is_chinese_or_english(texts[index]) == \"chinese\":\n",
    "            return [embed.cpu().tolist() for embed in get_embedding_for_chinese(get_luotuo_model(), texts)]\n",
    "        else:\n",
    "            return [get_embedding_for_english(text) for text in texts]\n",
    "    else:\n",
    "        if is_chinese_or_english(texts) == \"chinese\":\n",
    "            return get_embedding_for_chinese(get_luotuo_model(), texts)[0].cpu().tolist()\n",
    "        else:\n",
    "            return get_embedding_for_english(texts)\n",
    "```\n",
    "\n",
    "请为我编写这个函数的测试样例和测试代码，分别用两句中文和两句英文进行测试，并且输出四句句子两两之间的余弦相似度矩阵\n",
    "\n",
    "可以使用utils中的get_cosine_similarity计算相似度，请用python为我实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils import luotuo_openai_embedding, get_cosine_similarity\n",
    "\n",
    "texts = [\"我爱北京天安门\", \"天安门上太阳升\", \"I love London\", \"London Bridge is falling down\"]\n",
    "\n",
    "embeddings = []\n",
    "\n",
    "for text in texts:\n",
    "    embeddings.append(luotuo_openai_embedding(text))\n",
    "\n",
    "similarities = []\n",
    "for i in range(len(embeddings)):\n",
    "    row = []\n",
    "    for j in range(len(embeddings)):\n",
    "        if i != j:\n",
    "            row.append( get_cosine_similarity(embeddings[i], embeddings[j]))\n",
    "    similarities.append(row)\n",
    "\n",
    "print(similarities)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
